const express = require('express');
const router = express.Router();
const Blog = require('../../mongoModuels/rear/blog');
const time  = require('./../../utils/time');

router
.get('/navHaed', (req, res) => {
    let query = Blog.navHead.find({});
    query.exec({}, (err, data) => {
        if(err) {
            res.json({
                status: 0,
                message: err
            })
        } else {
            res.json({
                status: 1,
                message: '操作成功',
                data
            })
        }
    });
})
//获取导航

.post('/modNav', (req, res) => {
    if(req.body.name && req.body.id) {
        let creationTime = time(new Date().getTime());
        let data = {
            id: req.body.id,
            name: req.body.name,
            creationTime: creationTime
        }
        Blog.navHead.update({'_id': data.id}, {'name': data.name, 'creationTime': data.creationTime}, err => {
            if(err) {
                res.json({
                    status: 0,
                    message: '修改失败'
                })
            } else {
                res.json({
                    status: 1,
                    message: '修改成功'
                })
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//修改导航

.post('/delNev', (req, res) => {
    if(req.body.id) {
        Blog.navHead.remove({'_id': req.body.id}, err => {
            if(err) {
                res.json({
                    status: 0,
                    message: '删除失败'
                })
            } else{
                res.json({
                    status: 1,
                    message: '删除成功'
                })
            }
        })
    } else{
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//删除导航

module.exports = router;